package org.adream.account.service;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.adream.account.dao.EducationDao;
import org.adream.account.entity.EducationEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EducationService {
	
	@Autowired
	private EducationDao educationDao;
	
	@Autowired
	private HttpSession session;

	private final static Logger logger = LoggerFactory.getLogger(EducationService.class);
	
	/**
	 * 新增
	 * @param education
	 */
	public ResultModel<String> addEducation(EducationEntity education) {
		String operator = (String) session.getAttribute("uid");
		education.setCreator(operator);
		education.setModifier(operator);
		education.setDr(Constant.ONE);
		int result = educationDao.addEducation(education);
		if(result == 0) {
			logger.warn("新增教育程度数据失败,数据库错误,eName:{}", education.getEname());
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 删除 逻辑删除
	 * @param eid
	 */
	public ResultModel<String> deleteEducation(Integer eid) {
		EducationEntity education = educationDao.queryEduByEidAndDr(eid, Constant.ONE);
		if(education == null) {
			logger.warn("删除教育程度失败,数据不存在,eid:{}", eid);
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "删除失败,数据不存在", null);
		}
		String modifier = (String)session.getAttribute("uid");
		education.setModifier(modifier);
		education.setDr(Constant.ZERO);
		int result = educationDao.deleteEducation(education);
		if(result == 0) {
			logger.warn("删除教育程度数据失败,数据库错误,eid:{}", eid);
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 更新
	 * @param eid
	 * @param educationEntity
	 */
	public ResultModel<String> updateEducation(Integer eid,EducationEntity educationEntity) {
		EducationEntity education = educationDao.queryEduByEidAndDr(eid, Constant.ONE);
		if(education == null) {
			logger.warn("更新教育程度失败,数据不存在,eid:{}", eid);
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "更新失败,数据不存在", null);
		}
		String modifier = (String) session.getAttribute("uid");
		education.setEname(educationEntity.getEname());
		education.setModifier(modifier);
		int result = educationDao.updateEducation(education);
		if(result == 0) {
			logger.warn("更新教育程度数据失败,数据库错误,eid:{}", eid);
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 根据删除标志查询
	 * @param dr 删除标志
	 * @return
	 */
	public List<EducationEntity> queryEducationByDr(Integer dr) {
		return educationDao.queryEducationByDr(dr);
	}
	
	/**
	 * 根据eid,删除标志查询
	 * @param eid
	 * @param dr
	 * @return
	 */
	public EducationEntity queryEduByEidAndDr(Integer eid,Integer dr) {
		return educationDao.queryEduByEidAndDr(eid, dr);
	}
}